<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ed="livcos.org/ns/editor"
  xmlns:t="livcos.org/ns/cosmos/type"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:job="livcos.org/ns/process/job"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="ed t c doc job f">
	
	<xsl:import href="EditorContent.xsl"/>
  
	<xsl:param name="objId"/>
	<xsl:param name="scope" select="'content'"/>
	<xsl:param name="id"/>
	<xsl:param name="parentId"/>
	<xsl:param name="tmplObjId"/>
	<xsl:param name="tmplId"/>
	<xsl:param name="tmplArg1"/>
	<xsl:param name="tmplArg2"/>
	<xsl:param name="tmplArg3"/>
	<xsl:param name="tmplArg4"/>
	<xsl:param name="tmplArg5"/>
	
  <xsl:variable name="typeBase" select="'/livcos.org/type/editor'"/>
	
	<xsl:template match="/">
		<xsl:choose>
			<xsl:when test="$tmplObjId">
				<xsl:variable name="tmplObj" select="doc($tmplObjId)/*"/>
				<xsl:apply-templates select="if ($tmplId) then $tmplObj//*[@id = $tmplId] else ($tmplObj//*[@id])[1]" mode="info"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="obj" select="doc(concat($objId, '.', $scope))/*"/>
				<xsl:apply-templates select="f:node-by-id($obj, $id)" mode="info"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="@* | node()" mode="info">
		<xsl:variable name="type">
			<xsl:variable name="_type">
				<xsl:variable name="_nsPath" select="f:object-uri-from-namespace(namespace-uri())"/>
				<xsl:value-of select="$_nsPath"/>
				<xsl:if test="$_nsPath != ''">/</xsl:if>
				<xsl:value-of select="local-name()"/>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="local-name() = ''">
					<xsl:apply-templates select="." mode="type"/>
				</xsl:when>
				<xsl:when test="f:object-available(concat($typeBase, '/', $_type))">
					<xsl:value-of select="$_type"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="." mode="type"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="typeInfo" select="doc(concat($typeBase, '/', $type))/ed:editor"/>
		<entity type="{$type}" objId="{$objId}" scope="{$scope}" id="{$id}">
			<xsl:apply-templates select="$typeInfo/ed:editor | $typeInfo/ed:input">
				<xsl:with-param name="node" select="."/>
			</xsl:apply-templates>
			<menu>
				<xsl:apply-templates select="$typeInfo/ed:menu/*">
					<xsl:with-param name="node" select="."/>
				</xsl:apply-templates>
				<xsl:if test="not($typeInfo/ed:menu/*)">
					<action label="Edit..." type="ShowEditAction"/>
					<action label="Clone" type="CloneAction"/>
					<action label="Cut" type="CutAction"/>
					<action label="Copy" type="CopyAction"/>
					<action label="Paste" type="PasteAction"/>
					<action label="Paste into" type="PasteIntoAction"/>
					<action label="Delete" type="DeleteAction"/>
				</xsl:if>
			</menu>
		</entity>
	</xsl:template>
	
	<xsl:template match="text()" mode="type">
		<xsl:text>_default/text</xsl:text>
	</xsl:template>
	
	<xsl:template match="comment()" mode="type">
		<xsl:text>_default/comment</xsl:text>
	</xsl:template>
	
	<xsl:template match="@*" mode="type">
		<xsl:text>_default/attribute</xsl:text>
	</xsl:template>
	
	<xsl:template match="*" mode="type">
		<xsl:text>_default/element</xsl:text>
	</xsl:template>
	
	<xsl:template match="t:namespace" mode="type">
		<xsl:text>_default/namespace</xsl:text>
	</xsl:template>
	
	<xsl:template match="c:*" mode="type">
		<xsl:value-of select="namespace-uri()"/>
		<xsl:text>/ObjectNode</xsl:text>
	</xsl:template>
	
	
	<xsl:template match="ed:action">
		<xsl:param name="node"/>
		<action>
			<xsl:apply-templates select="@*">
				<xsl:with-param name="node" select="$node"/>
			</xsl:apply-templates>
			<xsl:variable name="ns_nodes" select="namespace::*[(local-name(.) != 'xml') and (local-name(.) != '')]"/>
			<xsl:for-each-group group-by="." select="$ns_nodes">
				<namespace prefix="{local-name(.)}" uri="{.}"/>
			</xsl:for-each-group>
		</action>
	</xsl:template>
	
	<xsl:template match="ed:action/@*">
		<xsl:copy-of select="."/>
	</xsl:template>
	
	<xsl:template match="ed:action/@enable" priority="2">
		<xsl:param name="node"/>
		<xsl:apply-templates select="$node" mode="enable">
			<xsl:with-param name="action" select=".."/>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="ed:editor | ed:input">
		<xsl:param name="node"/>
		<editor>
			<xsl:copy-of select="@*"/>
			<input type="hidden" name="objId" value="{$objId}"/>
			<input type="hidden" name="scope" value="{$scope}"/>
			<xsl:if test="normalize-space($id) != ''">
				<input type="hidden" name="id" value="{$id}"/>
			</xsl:if>
			<xsl:if test="$tmplObjId">
				<input type="hidden" name="id" value="{$parentId}"/>
				<input type="hidden" name="tmplObjId" value="{$tmplObjId}"/>
				<input type="hidden" name="tmplId" value="{$tmplId}"/>
			</xsl:if>
			<table class="ed_body">
				<col style="width: {if (@label-width) then @label-width else '15%'}"/>
				<tbody>
					<xsl:apply-templates select="*">
						<xsl:with-param name="node" select="$node"/>
					</xsl:apply-templates>
				</tbody>
			</table>
			<div class="ed_footer">
				<div id="_richtextToolbar">&#160;</div>
				<xsl:variable name="ns_nodes"
					select="../namespace::*[(local-name(.) != 'xml') and (local-name(.) != '')]"/>
				<xsl:for-each-group select="$ns_nodes" group-by=".">
					<input name="ns-{local-name(.)}" value="{.}" type="hidden"/>
				</xsl:for-each-group>
				<xsl:if test="namespace-uri-for-prefix('', ..) = ''">
					<input name="ns" value="" type="hidden"/>
				</xsl:if>
				<input type="submit" name="update" value="Update" class="ed_action"/>
				<input type="button" onclick="this.form._panel.close();" value="Cancel" class="ed_action"/>
			</div>
		</editor>
	</xsl:template>
	
	<!-- TODO: generated stylesheet for editor and actions (to resolve XPaths) -->
	<xsl:template match="*" mode="enable">
	</xsl:template>
	
	<xsl:template match="doc:image" mode="enable">
		<xsl:param name="action"/>
		<xsl:variable name="e" select="$action/@enable"/>
		<xsl:choose>
			<xsl:when test="$e = 'matches(@ref, ''https?://'')' and matches(@ref, 'https?://')"/>
			<xsl:otherwise>
				<xsl:attribute name="disabled">1</xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
</xsl:stylesheet>
